<template>
  <el-dialog :title=title
             v-if="open"
             :visible.sync="open"
             :modal= true
             width="80%"
             center
  >
  <div class="app-container">
    <el-tabs type="border-card">
      <el-tab-pane  :key="1" label="订单信息">
    <div style="font-size: 20px;">订单信息</div>
    <!--横线组件纯纯为了页面好看-->
    <el-divider></el-divider>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px" style="margin-left: 160px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="采购单号" prop="orderCode">
            <el-input v-model="form.orderCode" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="form.status" style="width: 100%;" disabled>
              <el-option label="已审批" :value=0 />
              <el-option label="采购入库" :value=1 />
              <el-option label="确认入库" :value=2 />
              <el-option label="待确定" :value=3 />
              <el-option label="待审批" :value=4 />
              <el-option label="已驳回" :value=5 />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="计划编号" prop="planNumber">-->
<!--            <el-input v-model="form.planNumber" readonly/>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="计划名称" prop="planName">-->
<!--            <el-input v-model="form.planName"  readonly />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row>
        <el-col :span="10">
          <el-form-item label="供应商" prop="vendorName">
            <el-input v-model="form.vendorName" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="供货日期" prop="materialDeadline">
            <el-date-picker clearable
                            v-model="form.materialDeadline"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择计划截止日期" style="width: 100%" readonly>
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
<!--      <el-row>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="开始日期" prop="planDate">-->
<!--            <el-date-picker readonly-->
<!--                            v-model="form.planDate"-->
<!--                            type="date"-->
<!--                            value-format="yyyy-MM-dd"-->
<!--                            placeholder="请选择计划开始日期" style="width: 100%">-->
<!--            </el-date-picker>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--        <el-col :span="10">-->
<!--          <el-form-item label="付款方式" prop="paymentType">-->
<!--            <el-select v-model="form.paymentType" placeholder="请选择付款方式" style="width: 100%;" disabled>-->
<!--              <el-option-->
<!--                v-for="dict in dict.type.erp_payment_type"-->
<!--                :key="dict.value"-->
<!--                :label="dict.label"-->
<!--                :value="parseInt(dict.value)"-->
<!--              ></el-option>-->
<!--            </el-select>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row>
        <el-col :span="10">
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="form.contact" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="联系人电话" prop="contactTel">
            <el-input v-model="form.contactTel" readonly/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="采购人员" prop="buyer">
            <el-input v-model="form.buyer" readonly/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="采购部门" prop="buyerDept">
            <el-input v-model="form.buyerDept" readonly/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="21">
          <el-form-item label="备注" prop="remarks">
            <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注"  style="width: 95%" maxlength="250" readonly/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <!--横线组件纯纯为了页面好看-->
    <el-divider></el-divider>
    <el-row style="margin-bottom: 20px;">
      <el-col :span="1.5">
        <div style="font-size: 20px; margin-top: 5px">采购物料</div>
      </el-col>
    </el-row>
    <el-table border :data="addGoodsList" show-summary :summary-method="getSummaries">
      <el-table-column label="计划编号" align="center" prop="planNumber"/>
      <el-table-column label="物料编号" align="center" prop="itemCode"/>
      <el-table-column label="物料名称" align="center" prop="itemName"/>
      <el-table-column label="规格型号" align="center" prop="specification"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
      <el-table-column label="分类" align="center" prop="itemTypeName"/>
      <el-table-column
        v-for="col in columnsConfig"
        :key="col.prop"
        :label="col.label"
        :prop="col.prop"
        :width="col.width"
        :align="col.align"
        v-if="col.show">
      </el-table-column>
      <el-table-column label="采购单价" align="center" prop="estimatedUnitPrice" />
      <el-table-column label="采购数量" align="center" prop="quantity" />
      <el-table-column label="客户" align="center" prop="clientName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="到货数量" align="center" prop="arrivedQuantity" />
      <el-table-column label="入库数量" align="center" prop="rkQuantity" />
      <el-table-column label="采购总价" align="center" prop="total" />
    </el-table>
      </el-tab-pane>
      <el-tab-pane  :key="2" label="审批流程" v-if="optType!=='add' && this.form.instanceId">
        <el-timeline id="approveTimeLine">
          <el-timeline-item
            v-for="item in approveFlow"
            :key="item.id"
            :timestamp="item.updateTime"
            placement="top"
            icon="el-icon-success"
            color="#13ce66"
          >
            <el-card>
              <h3>{{item.nodeName}}
                <b>{{item.approver}}</b>
                <span
                  style="font-weight:800;"
                  :style="{ color: item.skipType === 'PASS' ? 'green' : 'red' }">
                  {{item.skipType === 'PASS' ?'同意':'驳回'}}
                </span>
              </h3>
              <p style="padding-top: 20px;">审批意见：{{ item.message }}</p>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </el-tab-pane>
      <el-tab-pane   label="审批图" name="审批图" v-if="optType!=='add' && this.form.instanceId" >
        <img :src="imgUrl" width="100%" style="margin:0 auto"/>
      </el-tab-pane>
    </el-tabs>
    <div slot="footer" class="dialog-footer" style="text-align:center;margin-top: 20px;">
      <el-button type="danger" @click="submitForm" v-show="form.status===0 && optType==='warehousing'">确认采购入库</el-button>
      <el-button type="danger" @click="submitForm" v-show="(form.status===3 || form.status===5)&& optType==='confirm'">提交审批</el-button>
      <el-button type="info" @click="cancel">返 回</el-button>
    </div>
  </div>
  </el-dialog>
</template>

<script>
import {applyRecpt, getOrder,confirmOrder,approveFlow,listApproveOrder} from "@/api/mes/cg/order";
import {getColumnsConfigKey} from "@/api/system/columnsconfig";


export default {
  name: "infoView",
  dicts: ['erp_payment_type'],
  data() {
    return {
      open:false,
      optType: undefined,
      title:null,
      approveFlow:[{}],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 添加的商品信息表格数据
      addGoodsList: [],
      // 表单参数
      form: {},
      imgUrl: '',
      // 表单校验
      rules: {
      },
      columnsConfig:[],
    };
  },
  created() {
   /* this.getList();
    //查询商品表方法
    this.getGoodsList();
    //查询供应商列表方法
    this.getSupplierlist();*/
    this.getItemBase();
  },
  methods: {
    init(id){
      getOrder(id).then(response => {
        this.form = response.data;
        this.addGoodsList=response.data.cgPurchaseOrderDetailsList;
        if (this.form.instanceId){
          approveFlow(this.form.instanceId).then(re => {
            this.approveFlow = re.data.map(item => {
              const updateTime = item.updateTime;
              if (updateTime) {
                const date = new Date(updateTime);
                if (!isNaN(date.getTime())) {
                  item.updateTime = date.toISOString().slice(0, 19).replace('T', ' ');
                } else {
                  item.updateTime = '';
                }
              } else {
                item.updateTime = '';
              }
              return item;
            });
            listApproveOrder(this.form.instanceId).then(response => {
              this.imgUrl = "data:image/gif;base64," + response.data;
            });
          })
        }
      });
    },
    //商品小计
    getSummaries(param) {
      const {columns, data} = param;
      const sums = []; //声明变量
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "小计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (
          column.property === "total"
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index]+=' 元'
        } else {
          sums[index] = '';
        }
      });
      return sums;
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    submitForm() {
      let that = this;
       if (this.form.status === 0){
        this.$modal.confirm('是否同意编号为” ' + this.form.orderCode + ' ”的采购订单的入库申请？').then(function() {
          applyRecpt(that.form.id).then(response => {
            that.$modal.msgSuccess("申请入库成功");
            that.open = false;
            that.$parent.getList();
            that.cancel();
          })
        });
      }else if (this.form.status === 3 || this.form.status === 5) {
         this.$modal.confirm('是否确认并提交审批编号为” ' + this.form.orderCode + ' ”的采购订单？').then(function() {
           confirmOrder(that.form.id).then(response => {
             that.$modal.msgSuccess("操作成功");
             that.open = false;
             that.$parent.getList();
             that.cancel();
           })
         });
       }
    },
    getItemBase(){
      getColumnsConfigKey('MdItem_Base').then(res=>{
        this.columnsConfig = JSON.parse(res.data);
      })
    },
  }
};
</script>

<style >
#approveTimeLine .el-timeline-item__node--normal{
  width: 18px;
  height: 18px;
  left: -4px;
}
</style>
